package com.wyman.user_center_back.controller;

import com.wyman.user_center_back.domain.User;
import com.wyman.user_center_back.service.UserService;
import com.wyman.user_center_back.vo.UserLoginParam;
import com.wyman.user_center_back.vo.UserRegisterParam;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wyman.user_center_back.constans.UserConstants.USER_LOGIN_STATE;

/**
 * @author Wyman
 * @date 2024/9/1
 */

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/register")
    public Long userRegister(@RequestBody UserRegisterParam userRegisterParam){
        if(userRegisterParam==null){
//      TODO:后期统一添加处理
            return null;
        }
        String userAccount = userRegisterParam.getUserAccount();
        String userPassword = userRegisterParam.getUserPassword();
        String checkedUserPassword = userRegisterParam.getCheckedUserPassword();
        if(StringUtils.isAnyBlank(userAccount,userPassword,checkedUserPassword)){
//        TODO:后期统一添加处理
            return null;
        }
        return userService.userRegister(userAccount,userPassword,checkedUserPassword);
    }

    @PostMapping("/login")
    public Boolean userLogin(@RequestBody UserLoginParam userLoginParam, HttpServletRequest request){

        System.out.println("+++++++++++++++++++进入controller++++++++++++++++++++");

        if(userLoginParam==null){
//      TODO:后期统一添加处理
            return null;
        }
        String userAccount = userLoginParam.getUserAccount();
        String userPassword = userLoginParam.getUserPassword();
        if(StringUtils.isAnyBlank(userAccount,userPassword)){
//      TODO:后期统一添加处理
            return null;
        }
        return userService.userLogin(userAccount,userPassword,request);
    }

    @GetMapping("/currentUser")
    public User currentUser(HttpServletRequest request){

        if(request==null){
            //      TODO:后期统一添加处理
            return null;
        }
        return userService.getCurrentUser(request);
    }

    @GetMapping("/logout")
    public Boolean userLogout(HttpServletRequest request){

        if(request==null){
            //      TODO:后期统一添加处理
            return false;
        }
        request.getSession().removeAttribute(USER_LOGIN_STATE);
        return true;
    }

    @GetMapping("/getAllUser")
    public List<User> userGetAllUser(){
        return userService.getAllUser();
    }

    @PostMapping("/updateUser")
    public Boolean userUpdateUser(@RequestBody User user){

        if(user==null){
            return false;
        }
        return userService.updateUser(user);
    }

////    Restful风格
//    @GetMapping("/deleteUser/{id}")
//    public Boolean userDeleteUser(@PathVariable("id")Long id){
//
//        if(id==null){
//            return false;
//        }
//        return userService.deleteUser(id);
//    }

//    地址后跟参数
    @GetMapping("/deleteUser")
    public Boolean userDeleteUser(Long id){

     if(id==null){
            return false;
        }
        return userService.deleteUser(id);
    }

}
